﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Showroom.BLL;
using System.Net.Mail;
using System.IO;
using System.Net;

namespace MobileWeb
{
    public partial class ContactToUs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ((Site)Page.Master).SetTitlePage("Contact Us");
            }
        }

        public void Send_Email(string SmtpServer, string SendFrom, string SendTo, string Subject, string Body, string Name)
        {
            try
            {
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
                MailMessage msg = new MailMessage();
                String[] addr = SendTo.Split(';');
                for (int i = 0; i < addr.Length; i++)
                {
                    bool result = regex.IsMatch(SendTo);
                    if (result)
                    {
                        MailAddress to = new MailAddress(addr[i]);
                        msg.To.Add(to);
                    }
                }
                msg.From = new MailAddress(SendFrom);
                msg.IsBodyHtml = true;
                msg.Body = Body;
                msg.Subject = Subject;

                SmtpClient smtp = new SmtpClient();
                smtp.Host = SmtpServer;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential(Utilities.GetKeySetting("ContactEmail"), Utilities.GetKeySetting("ContactEmailPass"));
                smtp.Send(msg);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                //Send email
                string sSubject = txtTitle.Text.Trim();
                string sFrom = txtEmail.Text.Trim();
                string sTo = Utilities.GetKeySetting("ContactEmail");
                string sSMTPServer = Utilities.GetKeySetting("SMTPServer");
                //Generation body
                string formatMailBody = System.IO.File.ReadAllText(Server.MapPath("~/EmailTemplate/contact.html"));
                string sBody = formatMailBody.Replace("{0}", Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath + "/images/logo.png")
                    .Replace("{1}", txtYourName.Text.Trim())
                    .Replace("{2}", txtYourCompany.Text.Trim())
                    .Replace("{3}", txtPhone.Text.Trim())
                    .Replace("{4}", txtEmail.Text.Trim())
                    .Replace("{5}", txtAddress.Text.Trim())
                    .Replace("{6}", txtMessage.Text.Trim());

                Send_Email(sSMTPServer, sFrom, sTo, sSubject, sBody, txtYourName.Text.Trim());
                txtYourName.Text = txtYourCompany.Text = txtTitle.Text = txtMessage.Text = txtEmail.Text = txtAddress.Text = txtPhone.Text = string.Empty;
                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("alert('Thank you for contacting us');"), true);
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Contact.aspx.cs", "btnSubmit_Click", ex.StackTrace);
                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("alert('Oops! Send email failed. Please try again later.');"), true);
            }
        }
    }
}